#coding=utf-8
#Boa:FramePanel:PanelProductVariation

import wx

from product import CDataProduct
from MyListCtrl import MyListCtrlProSaleDetail, MyListCtrlProPurDetail

[wxID_PANELPRODUCTVARIATION, wxID_PANELPRODUCTVARIATIONBTNMANUALNUM, 
 wxID_PANELPRODUCTVARIATIONBTNMODIFYONHAND, 
 wxID_PANELPRODUCTVARIATIONTCMANUALNUM, wxID_PANELPRODUCTVARIATIONTCNAME, 
 wxID_PANELPRODUCTVARIATIONTCONHAND, wxID_PANELPRODUCTVARIATIONTCSPEC, 
 wxID_PANELPRODUCTVARIATIONTMANUALNUM, wxID_PANELPRODUCTVARIATIONTNAME, 
 wxID_PANELPRODUCTVARIATIONTONHAND, 
] = [wx.NewId() for _init_ctrls in range(10)]

class PanelProductVariation(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELPRODUCTVARIATION,
              name=u'PanelProductVariation', parent=prnt, pos=wx.Point(166,
              235), size=wx.Size(1105, 598), style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(1097, 564))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.tName = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONTNAME,
              label=u'\u5546\u54c1\uff1a', name=u'tName', parent=self,
              pos=wx.Point(16, 16), size=wx.Size(51, 16), style=0)

        self.tcName = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCNAME,
              name=u'tcName', parent=self, pos=wx.Point(72, 8),
              size=wx.Size(240, 26), style=0, value=u'')
        self.tcName.SetEditable(False)
        self.tcName.Enable(False)

        self.tOnhand = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONTONHAND,
              label=u'\u5e93\u5b58\uff1a', name=u'tOnhand', parent=self,
              pos=wx.Point(600, 16), size=wx.Size(51, 16), style=0)

        self.tcOnhand = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCONHAND,
              name=u'tcOnhand', parent=self, pos=wx.Point(656, 8),
              size=wx.Size(100, 26), style=0, value=u'')
        self.tcOnhand.SetEditable(False)
        self.tcOnhand.Enable(False)

        self.btnModifyOnhand = wx.Button(id=wxID_PANELPRODUCTVARIATIONBTNMODIFYONHAND,
              label=u'\u4fee\u6539', name=u'btnModifyOnhand', parent=self,
              pos=wx.Point(760, 8), size=wx.Size(48, 26), style=0)
        self.btnModifyOnhand.Bind(wx.EVT_BUTTON, self.OnBtnModifyOnhandButton,
              id=wxID_PANELPRODUCTVARIATIONBTNMODIFYONHAND)

        self.tcSpec = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCSPEC,
              name=u'tcSpec', parent=self, pos=wx.Point(312, 8),
              size=wx.Size(272, 26), style=0, value=u'')
        self.tcSpec.SetEditable(False)
        self.tcSpec.Enable(False)

        self.btnManualNum = wx.Button(id=wxID_PANELPRODUCTVARIATIONBTNMANUALNUM,
              label=u'\u4fee\u6539', name=u'btnManualNum', parent=self,
              pos=wx.Point(1032, 8), size=wx.Size(53, 26), style=0)
        self.btnManualNum.Bind(wx.EVT_BUTTON, self.OnBtnManualNumButton,
              id=wxID_PANELPRODUCTVARIATIONBTNMANUALNUM)

        self.tcManualNum = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCMANUALNUM,
              name=u'tcManualNum', parent=self, pos=wx.Point(928, 8),
              size=wx.Size(100, 26), style=0, value=u'')

        self.tManualNum = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONTMANUALNUM,
              label=u'\u624b\u5de5\u7f16\u53f7\uff1a', name=u'tManualNum',
              parent=self, pos=wx.Point(840, 16), size=wx.Size(85, 16),
              style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.lcPurDetail = MyListCtrlProPurDetail(self)
        self.lcPurDetail.SetSize(wx.Size(530, 500))
        self.lcPurDetail.Move(wx.Point(10, 50))
        
        self.lcSaleDetail = MyListCtrlProSaleDetail(self)
        self.lcSaleDetail.SetSize(wx.Size(530, 500))
        self.lcSaleDetail.Move(wx.Point(560, 50))
        
    def setData(self, proId):
        proObj = self.m_pro = CDataProduct.findObj(proId)
        
        self.tcName.SetValue(proObj.getValue(proObj.COL_NAME))
        self.tcSpec.SetValue(proObj.getValue(proObj.COL_SPEC))
        
        self.resetOnhand()
        self.resetManualNum()
        
        self.lcPurDetail.refresh({'proId':proId})
        self.lcSaleDetail.refresh({'proId':proId}) 
        
        self.m_pro = proObj

    def resetOnhand(self):
        proObj = self.m_pro
        onhand = u'%g%s' %(proObj.getValue(proObj.COL_ONHAND1), proObj.getValue(proObj.COL_UNIT1))
        self.tcOnhand.SetValue(onhand)
        
    def resetManualNum(self):
        proObj = self.m_pro
        manualNum = proObj.getValue(proObj.COL_MANUALNUM)
        if manualNum:
            self.tcManualNum.SetValue(manualNum)
        
    def OnBtnModifyOnhandButton(self, event):
        onhand = self.m_pro.getValue(self.m_pro.COL_ONHAND1)
        
        dlg = wx.TextEntryDialog(self, u'修改库存：', u'修改库存：', str(onhand))
        if (dlg.ShowModal() == wx.ID_OK):
            self.m_pro.setValue(self.m_pro.COL_ONHAND1, float(dlg.GetValue()))
            self.m_pro.update()
            
        self.resetOnhand()

    def OnBtnManualNumButton(self, event):
        manualNum = self.m_pro.getValue(self.m_pro.COL_MANUALNUM)
        if not manualNum:
            manualNum = ''
            
        dlg = wx.TextEntryDialog(self, u'修改手工编号：', u'修改手工编号：', manualNum)
        if (dlg.ShowModal() == wx.ID_OK):
            self.m_pro.setValue(self.m_pro.COL_MANUALNUM, dlg.GetValue())
            self.m_pro.update()
            
        self.resetManualNum()
